package com.pricl.frame.page.type;

import java.io.UnsupportedEncodingException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

@MappedTypes(String.class)
@MappedJdbcTypes(JdbcType.BLOB)
public class BlobTypeHandler extends BaseTypeHandler<String> {

	private static final String DEFAULT_CHARSET = "utf-8";

	@Override
	public void setNonNullParameter(PreparedStatement pstmt,
			int parameterIndex, String parameter, JdbcType jdbcType)
			throws SQLException {
		try {
			pstmt.setBytes(parameterIndex, parameter.getBytes(DEFAULT_CHARSET));
		} catch (UnsupportedEncodingException e) {
			pstmt.setBytes(parameterIndex, parameter.getBytes());
		}
	}

	@Override
	public String getNullableResult(ResultSet rs, String columnName)
			throws SQLException {
		byte[] bytes = rs.getBytes(columnName);
		try {
			return new String(bytes, DEFAULT_CHARSET);
		} catch (UnsupportedEncodingException e) {
			return new String(bytes);
		}
	}

	@Override
	public String getNullableResult(ResultSet rs, int columnIndex)
			throws SQLException {
		byte[] bytes = rs.getBytes(columnIndex);
		try {
			return new String(bytes, DEFAULT_CHARSET);
		} catch (UnsupportedEncodingException e) {
			return new String(bytes);
		}
	}

	@Override
	public String getNullableResult(CallableStatement cstmt, int columnIndex)
			throws SQLException {
		byte[] bytes = cstmt.getBytes(columnIndex);
		try {
			return new String(bytes, DEFAULT_CHARSET);
		} catch (UnsupportedEncodingException e) {
			return new String(bytes);
		}
	}

}